<?php

namespace app\model;

use app\common\OsType;
use app\traits\ModelTrait;
use think\Model;

class UserBehaviorLog extends Model
{
    use ModelTrait;

    public array $notes = [
        'status'      => [
            1 => '成功',
            2 => '失败',
        ],
        'user_status' => [
            1 => '正常',
            2 => '禁用',
        ],
        'type'        => [
            1 => '注册',
            2 => '登录',
            3 => '修改密码',
        ],
    ];

    public static function log(array $param = []): bool
    {
        $uid = $param['uid'] ?? 0;
        if (empty($uid)) return false;
        $type        = $param['type'] ?? 1;
        $status      = $param['status'] ?? 1;
        $login_type  = $param['login_type'] ?? 1;
        $user_status = $param['user_status'] ?? 1;
        $data        = [
            'uid'         => $uid,
            'type'        => $type,
            'status'      => $status,
            'user_status' => $user_status,
            'login_type'  => $login_type,
            'ip'          => request()->getRealIp(true),
            'os_type'     => request()->os_type ?? OsType::OS_TYPE_WEB->value,
            'create_time' => time(),
            'date_v'      => myDate(),
        ];
        self::create($data);
        return true;
    }

    public function userInfo(): \think\model\relation\HasOne
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,os_type,pitcher_id,launch_task_id,channel_id,status');
    }
}